// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   SourceFile

package com.sgame.android.network.util;

import com.sgame.android.network.Credentials;
import com.sgame.android.network.PairValue;
import com.sgame.android.network.util.SecretKey;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.Random;
import java.util.concurrent.TimeUnit;

// Referenced classes of package com.mobage.ww.android.network.util:
//            Credentials, a

public final class MakeRequestUrl
{
    private Credentials mCredentials;
    private ArrayList<PairValue> mUserEncodedValues;
    private ArrayList<PairValue> mOauthEncodedValues;
    private String mEncodeParams;
    private String mToken;
    private String mTimeStamp;

    public MakeRequestUrl(Credentials credentials)
    {
        mUserEncodedValues = new ArrayList<PairValue>();
        mOauthEncodedValues = new ArrayList<PairValue>();
        mCredentials = credentials;
        mEncodeParams = null;
        mToken = null;
    }

    public final String getStringParams(String method, String path, ArrayList<PairValue> paramArrayList)
    {
      if (this.mEncodeParams == null)
      {
        this.mEncodeParams = null;
        mUserEncodedValues = new ArrayList<PairValue>();
        mOauthEncodedValues = new ArrayList<PairValue>();

        makeUrlStringParams(method, path, paramArrayList);
        mEncodeParams = "OAuth ";
        Iterator<PairValue> item = mOauthEncodedValues.iterator();
        PairValue value;
        try {
          while (item.hasNext())
          {
        	  value = item.next();
           	mEncodeParams += ",";
            mEncodeParams += ((String)value.name + "=\"" + URLEncoder.encode((String)value.value, "UTF-8") + "\"");
          }
        } catch (UnsupportedEncodingException ex) {
        }
      }

      return this.mEncodeParams;
    }

    private void makeUrlStringParams(String method, String path, ArrayList<PairValue> arraylist) //b
    {
        Iterator<PairValue> item = arraylist.iterator();
        PairValue obj = null;
        String s2;
        try {
	        while(item.hasNext())
	        {
	        	obj = (PairValue)item.next();
	            s2 =(String) obj.name;
	            if(obj.value != null)
	            {
	            	mUserEncodedValues.add(new PairValue(URLEncoder.encode(s2, "UTF-8"), URLEncoder.encode((obj.toString()), "UTF-8")));
	            }
	        }

			mOauthEncodedValues.add(new PairValue("oauth_consumer_key", URLEncoder.encode(mCredentials.getConsumerKey(), "UTF-8")));
	        mOauthEncodedValues.add(new PairValue("oauth_nonce", URLEncoder.encode(getRandomString(), "UTF-8")));
	        mOauthEncodedValues.add(new PairValue("oauth_signature_method", URLEncoder.encode("HMAC-SHA1", "UTF-8")));
	        mOauthEncodedValues.add(new PairValue("oauth_timestamp", URLEncoder.encode(mTimeStamp == null ? Long.toString(TimeUnit.SECONDS.convert((new Date()).getTime(), TimeUnit.MILLISECONDS)) : mTimeStamp,  "UTF-8")));
	        mOauthEncodedValues.add(new PairValue("oauth_token", URLEncoder.encode(mCredentials.getOAuthToken(), "UTF-8")));
	        mOauthEncodedValues.add(new PairValue("oauth_version", URLEncoder.encode("1.0", "UTF-8")));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
        for(item = mOauthEncodedValues.iterator(); item.hasNext(); mUserEncodedValues.add(item.next()));
        String s3 = getUrlStringParams(method, path, mUserEncodedValues);
        StringBuilder sb = new StringBuilder();
        sb.append(mCredentials.getConsumerSecret());
        sb.append("&");
        sb.append(mCredentials.getOAuthTokenSecret());
        obj = new PairValue("oauth_signature", SecretKey.getKey(sb.toString(), s3));
        mUserEncodedValues.add(obj);
        mOauthEncodedValues.add(obj);
        return;
    }

    private String getUrlStringParams(String method, String path, ArrayList<PairValue> arraylist) //c
    {
        String s2;
        String s3;
        s2 = "";
        Comparator<PairValue> comparator = new Comparator<PairValue>() {

            public final int compare(PairValue obj, PairValue x1)
            {
                String s1 = x1.toString();
                String s2 = obj.toString();
                return s1.compareTo(s2);
            }
        };
        
        Collections.sort(arraylist, comparator);
        Iterator<PairValue> item = arraylist.iterator();
        StringBuilder sb = new StringBuilder();
        for(int i = 0; item.hasNext(); i++)
        {
            if(i != 0)
                sb.append("&");
            PairValue g1;
            g1 = (PairValue)item.next();
            String s4 = (String)g1.name;
            sb.append(s4).append("=").append((String)g1.value);
        }
        s2 = sb.toString();
        s3 = "";
        try {
			sb.append(URLEncoder.encode(method, "UTF-8")).append("&");
			sb.append(URLEncoder.encode(path, "UTF-8")).append("&");
			sb.append(URLEncoder.encode(s2.replaceAll("\\+", "\\%20").replaceAll("!", "\\%21").replaceAll("\\*", "\\%2A").replaceAll("'", "\\%27").replaceAll("\\(", "\\%28").replaceAll("\\)", "\\%29"), "UTF-8"));
			s3 = sb.toString();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return s3;
    }

    private String getRandomString() //a
    {
        if(mToken != null)
            return mToken;
        String s = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
        StringBuffer stringbuffer = new StringBuffer(6);
        Random random = new Random();
        for(int i = 0; i < 6; i++)
            stringbuffer.append(s.charAt(Math.abs(random.nextInt() % s.length())));

        return new String(stringbuffer);
    }

}
